package org.easyupdater;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.easyupdater.model.ApplicationSpec;
import org.easyupdater.model.BundleSpec;

/**
 * Class that handles the backup of the bundles
 * 
 * @author Bogdan Dimitriu
 */
public class BackupManager implements IBackupManager {

	private static final Logger log = Logger.getLogger(BackupManager.class);

	private File bundleBackupDirecory;

	private IApplicationSpecSerializer applicationSpecSerializer;

	private File applicationSpecFile;

	public BackupManager() {
		log.info("Creating BackupManager");
	}

	/**
	 * @see org.easyupdater.IBackupManager#backupApplication(ApplicationSpec)
	 */
	public boolean backupApplication(ApplicationSpec applicationSpec) {
		String backupVersion = applicationSpec.getVersion();
		File applicationBackupDirectory = new File(bundleBackupDirecory, backupVersion);
		boolean createSuccessful = applicationBackupDirectory.mkdirs();
		if (!createSuccessful) {
			log.warn("Could not create directory " + applicationBackupDirectory);
			return false;
		}
		File backupApplicationSpecFile = new File(applicationBackupDirectory, applicationSpecFile
				.getName());
		try {
			FileUtils.copyFile(applicationSpecFile, backupApplicationSpecFile);
		} catch (IOException e1) {
			log.error("Could not backup the application spec file. Cancelling backup");
			return false;
		}

		for (BundleSpec bundleSpec : applicationSpec.getBundleSpecs()) {
			File localJar = bundleSpec.getLocalJar();
			File backupFile = new File(applicationBackupDirectory, localJar.getName());
			if (backupFile.exists()) {
				log.info("Backed up file " + backupFile.getAbsolutePath()
						+ " already exists. It will be replaced");
				boolean deleted = backupFile.delete();
				if (!deleted) {
					log.warn("The backup file could not be replaced!");
					return false;
				}
			} else {
				try {
					FileUtils.copyFile(localJar, backupFile);
				} catch (IOException e) {
					log.warn("The backup file could not be copied!");
					return false;
				}
			}
		}

		return true;
	}

	public File getBundleBackupDirecory() {
		return bundleBackupDirecory;
	}

	public void setBundleBackupDirecory(File bundleBackupDirecory) {
		this.bundleBackupDirecory = bundleBackupDirecory;
	}

	public IApplicationSpecSerializer getApplicationSpecSerializer() {
		return applicationSpecSerializer;
	}

	public void setApplicationSpecSerializer(IApplicationSpecSerializer applicationSpecSerializer) {
		this.applicationSpecSerializer = applicationSpecSerializer;
	}

	public File getApplicationSpecFile() {
		return applicationSpecFile;
	}

	public void setApplicationSpecFile(File applicationSpecFile) {
		this.applicationSpecFile = applicationSpecFile;
	}

	@Override
	public boolean revertApplication(ApplicationSpec applicationSpec) {
		// TODO Auto-generated method stub
		return false;
	}

}
